#include  "gtest/gtest.h"

#include "infra_tiny_block.h"

TEST(tbTest, tbCreateDestroy0)
{
    int32_t ret = 0;
    infra_tb_handle handle = NULL;

    handle = infra_tb_create(10, 100, true, "test");
    EXPECT_TRUE((handle != NULL));
    
    ret = infra_tb_destroy(handle);
    EXPECT_EQ(ret, 0);
}

TEST(tbTest, tbCreateDestroy1)
{
    int32_t ret = 0;
    infra_tb_handle handle = NULL;

    handle = infra_tb_create(10, 100, false, "test");
    EXPECT_TRUE((handle != NULL));

    ret = infra_tb_destroy(handle);
    EXPECT_EQ(ret, 0);
}

TEST(tbTest, tbGetGiveback)
{
    int32_t ret = 0, idx = 0;
    infra_tb_handle handle = NULL;
    void *node[5] = {NULL};
    void *_node = NULL;

    handle = infra_tb_create(5, 100, true, "test");
    EXPECT_TRUE((handle != NULL));
    for (idx = 0; idx < 5; idx++) {
        node[idx] = infra_tb_get(handle);
        EXPECT_TRUE(node[idx] != NULL);
    }

    _node = infra_tb_get(handle);
    EXPECT_TRUE(_node == NULL);

    for (idx = 0; idx < 5; idx++) {
        ret = infra_tb_giveback(handle, node[idx]);
        EXPECT_TRUE(ret == 0);
    }

    infra_tb_destroy(handle);
}

TEST(tbTest, tbRecursive)
{
    int32_t ret = 0, idx = 0;
    infra_tb_handle handle = NULL;
    void *_node = NULL;

    handle = infra_tb_create(5, 100, true, "test");
    EXPECT_TRUE((handle != NULL));
    for (idx = 0; idx < 100; idx++) {
        _node = infra_tb_get(handle);
        EXPECT_TRUE(_node != NULL);
        ret = infra_tb_giveback(handle, _node);
        EXPECT_TRUE(ret == 0);
    }

    infra_tb_destroy(handle);
}

